package cn.rui.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.rui.common.Result;
import cn.rui.mapper.ImgFileMapper;
import cn.rui.pojo.ImgFile;
import cn.rui.pojo.User;
import cn.rui.pojo.vo.PageResult;
import cn.rui.service.ImgFileService;
import cn.rui.service.UserService;
import cn.rui.utils.QiniuUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徽州大都督
 * @date 2021/05/18
 */
@Service
@Transactional
public class ImgFileServiceImpl implements ImgFileService {

    @Autowired
    private ImgFileMapper imgFileMapper;

    @Autowired
    private UserService userService;

    @Override
    public Map findPage(PageResult pageResult) {
        Page<ImgFile> page = new Page<> (pageResult.getCurrentPage (), pageResult.getPageSize ());
        QueryWrapper<ImgFile> wrapper = new QueryWrapper<> ();
        wrapper.orderByDesc ("create_time");
        IPage<ImgFile> imgFileIPage = imgFileMapper.selectPage (page, wrapper);
        Map map = new HashMap ();
        map.put ("flag", true);
        map.put ("total", imgFileIPage.getTotal ());
        map.put ("data", imgFileIPage.getRecords ());
        return map;
    }

    @Override
    public Result upload(MultipartFile file) {
        //生成的是不带-的字符串，类似于：b17f24ff026d40949c85a24f4f375d42
        String uuid = IdUtil.simpleUUID ();
        String oldName = file.getOriginalFilename ();  //文件原始文件名

        int index = oldName.lastIndexOf (".");//获得文件后缀所在的下标
        String ext = oldName.substring (index - 1);  // .jpg
        String imgName = uuid + ext;

        //使用封装的七牛云工具类上传到云服务器
        try {
            QiniuUtils.upload2Qiniu (file.getBytes (), imgName);
        } catch (IOException e) {
            e.printStackTrace ();
            return Result.getFailure ("上传失败!");
        }
        String id = StpUtil.getLoginIdAsString ();
        User user = userService.findUserNameByUUid (id);
        ImgFile imgFile = new ImgFile ();
        imgFile.setImgName (imgName);
        imgFile.setLoginName (user.getUsername ());
        imgFileMapper.insert (imgFile);

        return Result.getSuccess ("上传成功!");
    }

    @Override
    public Result deleteById(Integer id) {
        ImgFile imgFile = imgFileMapper.selectById (id);
        imgFileMapper.deleteById (id);
        //删除七牛云存储文件
        QiniuUtils.deleteFileFromQiniu (imgFile.getImgName ());
        return Result.getSuccess ("删除成功!");
    }

    @Override
    public Result findAll() {
        //throw new MyException ("自定义异常!");
        List<ImgFile> imgFiles = imgFileMapper.selectList (null);
        return Result.getSuccess ("查询成功!", imgFiles);
    }
}
